<template>
  <rx-dialog @handOk="handleSubmit" @cancel="cancel">
    <rx-layout>
      <div slot="center">
        <a-form :form="form">
          <a-form-item style="display:none">
            <a-input v-decorator="['boxId']"/>
          </a-form-item>
          <a-row>
            <a-col :span="12">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="i18n('form.key','标识键')">
                <a-input style="width: 80%;" :placeholder="i18n('form.key','标识键')"
                         v-decorator="['key', {rules: [
                             {required: true, message: i18n('form.key','标识键','input')},
                             {pattern:/^[a-zA-Z][a-zA-Z0-9_-]*$/,message:i18n2('form.str','不能以数字开头,不能包含【_-】以外的特殊字符！','input')}
                             ]}]"/>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="i18n('form.name','名字')">
                <a-input style="width: 80%;" :placeholder="i18n('form.name','名字')"
                         v-decorator="['name', {rules: [{required: true, message: i18n('form.name','名字','input')}]}]"/>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
        <ins-msg-def-list01 ref="defList" @changeMsfDdfList="resultMsfDdfList" :msgDefLoadData="msgDefLoadData"/>
      </div>
    </rx-layout>



  </rx-dialog>
</template>
<script>
  import InsMsgboxDefApi from '@/api/portal/core/insMsgboxDef';
  import InsMsgboxBoxApi from '@/api/portal/core/insMsgboxBox'
  import {BaseForm, RxDialog, Util} from "@lowcode/jpaas-common-lib";
  import InsMsgDefList01 from './InsMsgDefList01.vue'
  import ARow from "ant-design-vue/es/grid/Row";
  import ACol from "ant-design-vue/es/grid/Col";


  export default {
    name: 'InsMsgboxDefEdit',
    mixins: [BaseForm],
    components: {
      ACol,
      ARow,
      RxDialog, InsMsgDefList01, Util, InsMsgboxBoxApi
    },
    data() {
      return {
        msgDefLoadData: []
      }
    },
    methods: {
      i18n(name,text,key){
          return this.$ti18(name,text,"InsMsgboxDef",key);
      },
        i18n2(name,text,key){
            return this.$ti18(name,text,"formBoEntity",key);
        },
      onload_(resultData) {
        if (typeof (resultData) == 'undefined') {
          return;
        }
        InsMsgboxBoxApi.queryByBoxId(resultData.boxId).then(res => {
          if (!res.success) {
            return;
          }
          var data = res.data;
          for (var i = 0; i < data.length; i++) {
            var insMsgDef = {
              index: i,
              content: data[i].content,
              msgId: data[i].msgId
            };
            this.msgDefLoadData.push(insMsgDef);
          }
        });
      },
      getMsgDefIdList() {
        var list = [];
        for (var i = 0; i < this.msgDefLoadData.length; i++) {
          list.push(this.msgDefLoadData[i].msgId);
        }
        return list.join(",");
      },
      resultMsfDdfList(list) {
        this.msgDefLoadData = list;
      },
      get(id) {
        return InsMsgboxDefApi.get(id);
      },
      save(values) {
        values.colId = this.getMsgDefIdList();
          //新增时保存应用ID
          let appId=this.curApp.appId;
          if(appId ){
              values.appId=appId;
          }
        InsMsgboxDefApi.saveOne(values).then(res => {
          if (!res.success) {
            this.$message.error(res.message);
            return;
          }
          Util.closeWindow(this, "ok");
        });
      },
    }
  }
</script>
